package com.february.todo.tools;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.annotation.Nullable;

import com.february.todo.R;
import com.february.todo.MainActivity;
import com.february.todo.SettingActivity;
import com.february.todo.StatisticsActivity;
import com.february.todo.TodoListActivity;

import java.util.ArrayList;
import java.util.List;

public class NavigateBar extends LinearLayout {

    private Spinner spinner;
    private Context context;

    public NavigateBar(Context context) {
        super(context);
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public NavigateBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.navigate_bar, this);
        spinner = findViewById(R.id.bar_spinner);

        List<String> list = new ArrayList<String>();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NavigateBar);
        int activityID = typedArray.getInteger(R.styleable.NavigateBar_activity_id, -1);
        switch (activityID) {
            case 0:
                list.add("设置");
                list.add("列表");
                list.add("统计信息");
                initSpinerListener(0);
                break;
            case 1:
                list.add("设置");
                list.add("主页");
                list.add("统计信息");
                initSpinerListener(1);
                break;
            case 2:
                list.add("设置");
                list.add("列表");
                list.add("统计信息");
                initSpinerListener(2);
                break;
            default:
                break;
        }
        typedArray.recycle();

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, R.layout.support_simple_spinner_dropdown_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        spinner.setSelection(0, false);
    }

    void initSpinerListener(int activityID) {
        switch (activityID) {
            case 0:
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                        if (position == 0) {
//                            Intent intent = new Intent(context, SettingActivity.class);
//                            context.startActivity(intent);
//                            spinner.setSelection(0, false);
//                        }
//                        else
                            if (position == 1) {
                            Intent intent = new Intent(context, TodoListActivity.class);
                            context.startActivity(intent);
                            spinner.setSelection(0, false);
                        } else if (position == 2) {
                            Intent intent = new Intent(context, StatisticsActivity.class);
                            context.startActivity(intent);
                            spinner.setSelection(0, false);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            case 1:
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 1) {
                            Intent intent = new Intent(context, MainActivity.class);
                            context.startActivity(intent);
                            spinner.setSelection(0, false);
                        } else if (position == 2) {
                            Intent intent = new Intent(context, StatisticsActivity.class);
                            context.startActivity(intent);
                            spinner.setSelection(0, false);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            case 2:
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 1) {
                            Intent intent = new Intent(context, MainActivity.class);
                            context.startActivity(intent);
                            spinner.setSelection(0, false);
                        } else if (position == 2) {
                            Intent intent = new Intent(context, TodoListActivity.class);
                            context.startActivity(intent);
                            spinner.setSelection(0, false);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                break;
            default:
                break;
        }
    }
}
